<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <div class="filter-item">供應商銷量統計時間:</div>
      <date-range-picker
        v-model="modelEndDate"
        clearable
        placeholder="时间"
        class="filter-item"
        :fix-date.sync="timeSlot"
        @keyup.enter.native="crud.toQuery"
        @change="getTimeSlot"
      />
      <el-select
        v-model="query.vendorId"
        placeholder="供應商"
        clearable
        class="filter-item"
        filterable
        @keyup.enter.native="crud.toQuery"
      >
        <el-option
          v-for="item in vendor"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <label class="el-form-item-label" />
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation>
        <template #right>
          <el-button
            class="filter-item"
            size="mini"
            type="warning"
            :disabled="modelEndDate.length < 1"
            @click="exportVendorSale"
          >導出
          </el-button>
          <rrOperation :crud="crud" />

        </template>
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        row-key="id"
        :data="crud.data"
        stripe
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="sku" label="产品编号" />
        <el-table-column prop="upc" label="upc" />
        <el-table-column prop="productName" label="产品名称" width="200px" />
        <el-table-column prop="price" label="单价" align="right" />
        <el-table-column prop="cost" label="成本" align="right" />
        <el-table-column prop="totalAmount" label="总价" align="right" />
        <el-table-column prop="vendor" label="供应商" />
        <el-table-column prop="productCategory" label="种类" />
        <el-table-column prop="quantity" label="数量" align="right" />
        <el-table-column prop="time" label="入库时间" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudReport from '@/api/fms/report'
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import dateRangePicker from '@/components/DateRangePicker'
import { downloadFile } from '@/utils'
import { getAll } from '@/api/cms/vendor'
export default {
  name: 'VendorSale',
  components: { pagination, crudOperation, rrOperation, dateRangePicker },
  mixins: [presenter(), header(), crud()],
  dicts: ['true_false'],
  cruds() {
    return CRUD({
      title: '訂單銷售統計表',
      url: 'report/vendorSale',
      sort: [],
      crudMethod: { ...crudReport },
      optShow: {},
      query: { startTime: '', endTime: '' },
      queryOnPresenterCreated: false
    })
  },
  data() {
    return {
      modelEndDate: [],
      timeSlot: '',
      vendor: []
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi', 'baseApi'])
  },
  created() {
    this.getVendor()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getTimeSlot(sel) {
      this.query.startTime = sel[0]
      this.query.endTime = sel[1]
    },
    exportVendorSale() {
      if (this.query.vendorId == null && this.query.startTime == null && this.query.endTime == null) {
        this.$notify({
          title: '警告',
          message: '请先选择供应商和時間',
          type: 'warning'
        })
        return
      }
      crudReport.exportVendorSale({
        vendorId: this.query.vendorId,
        startTime: this.query.startTime,
        endTime: this.query.endTime
      }).then(res => {
        if (res.code !== undefined && res.code !== 0) {
          this.$notify({
            title: '失敗',
            message: res.message,
            type: 'error'
          })
          return
        }
        downloadFile(res, this.crud.title + '数据', 'xlsx')
      })
    },
    getVendor() {
      getAll().then(res => {
        if (res.code === 0) {
          this.vendor = res.data
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
